(function ($) {
    //-------------------------ajax队列---------------------------------------------//
    //errorDeal 0 事件不处理 1 事件插到末位 2 事件顺序不变 继续执行
    //{queue:[{url:'',data:'',dataType:'',success:success,error:error,errorDeal:,type:'post',jsonp:'',successparam:[]}] }

    $.AjaxQueue = function (opts) {
        var source = this;
        if (!opts.queue) {
            opts.queue = [];
        } else {
            $.AjaxQueue.opts = opts;
        }
    }

    $.AjaxQueue.opts = [];
    $.AjaxQueue({ queue: [] });
    $.AjaxQueue.execAjax = function () {
        var opts = $.AjaxQueue.opts;
        var dft = { errorDeal: 0, type: 'post', dataType: 'text' };
        var ajaxOptions = {};
        var item = $.extend(dft, opts.queue[0]);
        ajaxOptions.url = item.url || "";
        ajaxOptions.data = item.data || {};
        ajaxOptions.dataType = item.dataType || "text";
        //ajaxOptions.errorDeal = item.
        ajaxOptions.success = function (data) { if (item.success) { item.success.apply(this, arguments); }; opts.queue.shift(); if (opts.queue.length > 0) $.AjaxQueue.execAjax(); }
        ajaxOptions.type = item.type || "post";
        ajaxOptions.error = function (data) { if (item.error) {  item.success.apply(this, arguments); }; if (item.errorDeal == 1) { var l = opts.queue.shift().shift(); opts.queue.push(l) } else if (item.errorDeal == 2) { } else { opts.queue.shift() }; if (opts.queue.length > 0) $.AjaxQueue.execAjax(); }
        if (ajaxOptions.dataType === "jsonp")
            ajaxOptions.jsonp = item.jsonp;
        //$.ajax(ajaxOptions);
    }



    //-------------------------ajax队列end---------------------------------------------//

    //-------------------------验证----------------------------------------------------//
    $.fn.EmptyValidate = function (opts) {
        opts = opts || { errorMsg: "不能为空", direction: 1 };
        opts.errorMsg = opts.errorMsg || "不能为空";
        $(this).bind("blur", function () {
            var source = $(this);
            if (source.val() == 0) {
                $.Tips(this, opts.errorMsg, opts.direction);
                return;
            }
        })
        return $(this);
    }

    $.fn.RegexValidate = function (opts) {
        if (!opts || !opts.regex)
            return $(this);
        //opts = opts || { errorMsg: "不能为空", direction: 1, regex: /\w/g };
        opts.errorMsg = opts.errorMsg || "不能为空";
        $(this).bind("blur", function () {
            var source = $(this);
            if (!opts.regex.test(source.val())) {
                $.Tips(this, opts.errorMsg, opts.direction);
                return;
            }
        });
        return $(this);
    }

    //opts{{ errorMsg: "不能为空", direction: 1, func: function(){} };}
    $.fn.FuncValidate = function (opts) {
        if (!opts) {
            $.Tips(this, "参数错误", 1);
            return;
        }
        $(this).bind("blur", function () {
            var source = $(this);
            var msg = opts.func.apply(this, [source.val(), source])
            if (msg != "true") {
                $.Tips(this, msg, opts.direction);
                return;
            }
        })
        return $(this);
    }

    $.Tips = function (obj, msg, ftype) {
        var tipId, ftype = ftype || 1;
        var source = $(obj);
        if (obj.id) {
            tipId = "tips_" + obj.id;
        } else {
            tipId = "tips_c_" + ++$.Tips.count;
            obj.id = "obj_c_" + ++$.Tips.count;
        }
        if ($("#" + tipId).length == 0) {
            var html = [];
            html.push('<div id="' + tipId + '" class="div-tips" style="background: none repeat scroll 0 0 #FEF1F1;border: 1px solid #CE0000;font-size:12px;display:none;position:absolute;height:auto;z-index:1000;padding:2px 5px;color: #CE0000;line-height: 16px;">');
            html.push('</div>');
            $("body").append(html.join());
        }
        var offset = source.offset();
        var left = ftype == 1 ? (offset.left + 5) : (offset.left + source.width() + 10);
        var top = ftype == 1 ? (offset.top + source.height() + 8) : (offset.top + 8);
        var tips = $("#" + tipId);
        tips.css({ left: left + "px", top: top + "px" }).html(msg);
        tips.show();
        source.bind("focus", function () { $("#" + tipId).hide(); });
    }
    $.Tips.count = 0;
    //-------------------------验证end----------------------------------------------------//

    //----------------------------图片延迟加载--------------------------------------------//
    $.lazyimg = function (opts) {
        opts = opts || { selector: "img", tag: "_src", tagClass: "lazy-img" };
        opts.selector = opts.selector || "img";
        opts.tag = opts.tag || "_src";
        $(opts.selector).addClass("lazy-img");
        $.lazyimg.ChangeImg = function () {
            $(opts.selector).each(function () {
                var offset = $(this).offset();
                var imgtop = offset.top;
                var windowHeight = $(window).height();
                var scrollheight = $(document).scrollTop();
                var screenbottom = windowHeight + scrollheight;
                if (imgtop < screenbottom) {
                    $(this).attr("src", $(this).attr(opts.tag)).removeClass(opts.tagClass);
                }
                //$("#spn_a").html(scrollheight);
            });
            if ($("." + opts.tagClass).length == 0)
                $(window).unbind("scroll", $.lazyimg.ChangeImg);

        }
        $(window).bind("scroll", $.lazyimg.ChangeImg);
    }
    //----------------------------图片延迟加载end--------------------------------------------//

    //--------------绑定propertychange时间 兼容ff ie func内不能使用this----------------------//
    $.fn.inputchange = function (func) {
        var typeName = this.attr("type");
        if (typeName)
            typeName = typeName.toLowerCase();
        if ("text" == typeName) {
            if ($.browser.msie) {
                this[0].attachEvent("onpropertychange", func, false);
            } else {
                this[0].addEventListener("input", func, false);
            }
        }
    }
    //--------------绑定propertychange end-------------------------------------------------//

    //--------------浮动-------------------------------------------------------------------//
    //opts {title:selector, boundaryid:selector, posX:0, pxsY:0,z:0}
    $.fn.drag = function (opts) {
        //var def = { boundaryid: "body", posX: 0, poxsY: 0 };
        var opt = {};
        $.extend(opt, $.fn.drag.def, opts);
        var objTarget = $(this);
        var objBoundary = $(opt.boundaryid);
        var objTitle;

        if (!opt.title) {
            objTitle = $(this);
        } else {
            objTitle = $(opt.title);
        }
        var borderX = objBoundary.offset().left;
        var borderY = objBoundary.offset().top;
        var borderEX = borderX + objBoundary.width() - objTarget.width();
        var borderEY = borderY + objBoundary.height() - objTarget.height();
        //console.log("borderEY->" + objBoundary.offset().top);
        var move = function (e) {
            var x = e.clientX - opt.posX;
            var y = e.clientY - opt.posY;
            if (x >= borderX && x <= borderEX)
                objTarget.css("left", x + "px");
            else if (x < borderX)
                objTarget.css("left", borderX + "px");
            else
                objTarget.css("left", borderEX + "px");
            if (y >= borderY && y <= borderEY)
                objTarget.css("top", y + "px");
            else if (y < borderY)
                objTarget.css("top", borderY + "px");
            else
                objTarget.css("top", borderEY + "px");

        };
        objTitle.bind("mousedown", function (e) {
            //console.log(objTitle.attr("id"));
            opt.posX = e.clientX - objTarget.offset().left;
            opt.posY = e.clientY - objTarget.offset().top;
            $(document).bind("mousemove", move);
            if ($.browser.mozilla) {
                e.preventDefault();
            }
        }).css({ "cursor": "move" });
        $(document.body).bind("mouseup", function () {
            $(document).unbind("mousemove", move);
        })
        this.css({ "left": opt.posX, "top": opt.posY, "position": "absolute" });
        //, "z-index": (++$.fn.drag.index)
        if (opt.z) {
            this.css({ "z-index": opt.z });
        } else {
            this.css({ "z-index": (++$.fn.drag.index) });
        }
        return $(this);
    }
    $.fn.drag.def = { boundaryid: "body", poX: 0, poxY: 0 };
    $.fn.drag.index = 1000;
    //--------------浮动End---------------------------------------------------------------------//

    //------------------NewSelect---------------------------------------------------------------//
    //{change:func }
    $.fn.newSelect = function (opts) {
        var opt = $.extend({ width: 0 }, opts);

        for (var i = 0; i < this.length; i++) {
            var source = this.eq(i);
            var sourceId = source.attr("id");
            if (sourceId) {
                $("#dl_new_" + sourceId).remove();
            }
            $.newSelect.BuildSelect(source, opt);
        }
        return this;
    }

    $.newSelect = {
        CreateContainer: function (source, opt) {
            var sourceId = source.attr("id");
            sourceId = sourceId || "slc_old_" + $.newSelect.index++;
            source.attr("id", sourceId);
            var newId = "dl_new_" + sourceId;
            var html = '<dl id="' + newId + '"  class="tovisit"style=" position:relative; z-index:1000; display:inline-block;zoom:1; *display:inline; border-bottom:1px solid #83ACC6; border-top:1px solid #83ACC6;"></dl>';
            var $_obj = $(html);
            return $_obj;
        },
        CreateSelected: function (source, opt) {
            var html = '<dt style=" border-bottom:0; overflow:hide" ><span  class="arrow-down"  style="font-family:Tahoma,Verdana,STHeiTi,simsun,sans-serif;">';
            html += source.find("option:selected").html();
            html += '</span>';
            var $_obj = $(html);
            return $_obj;
        },
        CreateOptions: function (source, opt) {
            var options = source.find("option");
            var html = [];
            html.push('<dl  class="tovisit" style=" display:none;position:absolute;left:0;top:27px;border-bottom:1px solid #83ACC6; overflow:hidden">');
            for (var i = 0; i < options.length; i++) {
                var item = $(options[i]);
                html.push('<dd ><a  href="javascript:void(0)" _val="');
                html.push(item.attr("value"));
                html.push('" >');
                html.push(item.html());
                html.push('</a></dd>');
            }
            html.push("</dl>");
            $_option = $(html.join(""));
            return $_option;
        },
        BuildSelect: function (source, opt) {
            var $_container = $.newSelect.CreateContainer(source, opt);
            var $_options = $.newSelect.CreateOptions(source, opt);
            var $_selected = $.newSelect.CreateSelected(source, opt);
            if (!(opt.width == 0 || isNaN(opt.width))) {
                $_container.width(opt.width);
            }
            $_container.append($_selected);
            $_container.append($_options);
            source.before($_container);
            $_options.width($_container.outerWidth(true));
            $_selected.width($_container.width() - 12);
            source.hide();
            $_container.hover(
                function () {
                    $(this).addClass("selected");
                    $(this).find("dl").show();
                    source.trigger("mouseinner");
                },
                function () {
                    $(this).removeClass("selected");
                    $(this).find("dl").hide();
                    source.trigger("mouseleave");
                }
            );
            $_options.find("a").bind("click", function () {
                var val = $(this).attr("_val");
                var text = $(this).html();
                $_selected.find("span").html(text).attr("_val", val);
                source.val(val);
                source.trigger("change");
                $_options.hide();
            });
            $_container.bind("click", function () {
                source.trigger("click");
            })

        },
        index: 0
    };
    //-------------------------------------------------------------------------------------------//
})(jQuery)